package com.sixmac.core.beetl.tag;

import com.sixmac.core.aop.AopContext;
import com.sixmac.core.constant.ConstCache;
import com.sixmac.core.constant.ConstCacheKey;
import com.sixmac.core.constant.Cst;
import com.sixmac.core.meta.IQuery;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.plugins.dao.Md;
import com.sixmac.core.shiro.ShiroKit;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.Func;
import com.sixmac.core.toolbox.cache.CacheKit;
import com.sixmac.core.toolbox.cache.ILoader;
import com.sixmac.core.toolbox.kit.ClassKit;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.core.toolbox.kit.StrKit;
import org.beetl.core.Tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class SelectTag extends Tag {

    @Override
    @SuppressWarnings("unchecked")
    public void render() {
        try {
            Map<String, String> param = (Map<String, String>) args[1];

            final String code = param.get("code");
            String name = param.get("name");
            String value = Func.toStr(param.get("value"));
            String token = (StrKit.notBlank(value)) ? "" : Func.toStr(param.get("token"));
            String type = param.get("type");
            String where = param.get("where");
            String required = param.get("required");
            String tail = param.get("tail");
            String inter = param.get("intercept");
            String sql = "";

            Map<String, Object> modelOrMap = CMap.createHashMap();

            IQuery intercept = Cst.me().getDefaultQueryFactory();

            String CACHE_NAME = ConstCache.SYS_CACHE;

            if (type.equals("dict")) {
                sql = "select num as ID,pId as PID,name as TEXT from  BLADE_DICT where code='" + code + "' and num > 0 order by num asc";
                intercept = Cst.me().getDefaultSelectFactory().dictIntercept();
            } else if (type.equals("user")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ID,name as TEXT from  BLADE_USER where status=1";
                intercept = Cst.me().getDefaultSelectFactory().userIntercept();
            } else if (type.equals("dept")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ID,PID,SIMPLENAME as TEXT from  BLADE_DEPT";
                intercept = Cst.me().getDefaultSelectFactory().deptIntercept();
            } else if (type.equals("site")) {
                CACHE_NAME = ConstCache.CUSTOM_CACHE;
                sql = "select id as ID,name as TEXT from t_region";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            } else if (type.equals("role")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ID,name as TEXT from  BLADE_ROLE";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            } else if (type.equals("diy")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                type = type + "_" + param.get("source");
                if (StrKit.notBlank(where)) {
                    modelOrMap = JsonKit.parse(where, Map.class);
                }
                sql = Md.getSql(param.get("source"));
            } else if (type.equals("animationCatelogy")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ID,name as TEXT from t_animation_category where type = 0 and `status` = 1 ORDER BY createDate DESC";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();

            } else if (type.equals("animationAlbum")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ID,name as TEXT from t_animation_album ORDER BY createDate DESC";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            } else if (type.equals("appuser")) {
                CACHE_NAME = ConstCache.CUSTOM_CACHE;
                sql = "select id as ID,name as TEXT from t_app_user ORDER BY createDate DESC";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            } else if (type.equals("audioCategory")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ID,category as TEXT from t_audio_category where type = 1 and `status` = 1 ORDER BY createDate DESC";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            } else if (type.equals("animation")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ani.ID,ani.name as TEXT from t_animation ani where ani.id not in (select an.id from t_animation an,t_animation_audio aa where an.status = 1 and an.id = aa.animaId) ORDER BY ani.createDate DESC";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            } else if (type.equals("allAnimation")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ani.ID,ani.name as TEXT from t_animation ani ORDER BY ani.createDate DESC";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            } else if (type.equals("audioAlbum")) {
                CACHE_NAME = ConstCache.SYS_CACHE;
                sql = "select ID,name as TEXT from t_audio_album where `status` = 1 ORDER BY createDate DESC";
                intercept = Cst.me().getDefaultSelectFactory().roleIntercept();
            }


            if (StrKit.notBlank(inter)) {
                intercept = ClassKit.newInstance(inter);
            }

            final String _sql = sql;
            final Map<String, Object> _modelOrMap = modelOrMap;
            final IQuery _intercept = intercept;

            List<Map<String, Object>> dict = CacheKit.get(CACHE_NAME, ConstCacheKey.DICT + type + "_" + code + "_" + ShiroKit.getUser().getId(), new ILoader() {
                @Override
                public Object load() {
                    return Db.selectList(_sql, _modelOrMap, new AopContext(), _intercept);
                }
            });

            StringBuilder sb = new StringBuilder();
            String[] arr = name.split("\\.");
            String sid = arr[0];
            if (arr.length == 2) {
                sid = arr[1];
            }
            sid = "_" + sid;
            sb.append("<select onchange=\"" + sid + "_selectChanged('" + sid + "')\" " + required + " class=\"form-control\" id=\"" + sid + "\"  name=\"" + token + name + "\">");
            if (type.equals("audioAlbum")) {
                sb.append("<option value>无</option>");
            } else {
                sb.append("<option value></option>");
            }


            for (Map<String, Object> dic : dict) {
                String id = Func.toStr(dic.get("ID"));
                String selected = "";
                if (Func.equals(id, value)) {
                    selected = "selected";
                }
                sb.append("<option " + selected + " value=\"" + id + "\">" + dic.get("TEXT") + "</option>");
            }
            sb.append("</select>");


            sb.append("<script type=\"text/javascript\">");
            sb.append("		function " + sid + "_selectChanged(sid) {");
            sb.append("			$('#form_token').val(1);");
            sb.append("			$('#' + sid).attr('name','" + name + "');");
            if (StrKit.notBlank(tail)) {
                sb.append("			var options=$('#' + sid + ' option:selected');");
                sb.append("			$('#' + sid + '_ext').val(options.text());");
            }
            sb.append("		};");
            sb.append("</script>");
            if (StrKit.notBlank(tail)) {
                sb.append("<input type=\"hidden\" id=\"" + sid + "_ext\" name=\"" + name.split("\\.")[0] + "." + tail + "\">");
            }

            ctx.byteWriter.writeString(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
